<?php
use Fox\Dao\SqlFragment;
use Fox\Html\Input;
use Fox\Html\TextArea;
use Doctrine\DBAL\Connection;
use Fox\Generator;
use Fox\Html\DropList;
use Fox\Html\Screen;

$xview = Xview::instance();
$id = $xview->val('grouping');
$target = $xview->val('jsondata');
if(isset($_GET['jsondata'])) {
	unset($_GET['jsondata']);
}

$excluded = array();

$searchHelp = new Fox\Html\SearchHelp('Users');
$searchHelp->width(500);
$searchHelp->setMultiple(new Fox\Html\Button('select',"ReloadFromSelectLines('#b_GRUsers','MC_gTarget',
								'?mod=users&act=groups&sac=users','App.close(\'sh:Users\')','InputSerialize(\'sNotes\')');"));
$searchHelp->switchToObjectMode();
$ilink = $xview->currentURL();
$searchHelp->getSelBlock()->add(new TextArea('excluded'))
							->excluded()
							->hidden();
$searchHelp->getSelBlock()->add(new Fox\Html\Input('idusr',false,$id))
							->excluded()
							->hidden();
$searchHelp->getSelBlock()->item('email')->size(Input::SIZE_MEDIUM);
// Load already selected Product
$gItems = Xview::instance()->component('users', 'groupsUsers'); 

$data = $gItems->get(true);

if(is_array($data['gTarget']))
{
	foreach($data['gTarget'] as $line)
	{
		if(isset($line['idusr']))
			$Ids[] = $line['idusr'];
	}
	
	if(!empty($id))
		$Ids[] = $id;
	if(isset($Ids))
	{
		$excluded = new \Fox\Dao\SqlFragment('t.idusr NOT IN (?) ',array($Ids),array(Connection::PARAM_STR_ARRAY));
		$searchHelp->getSelBlock()->item('excluded')->default = addslashes(json_encode($Ids));
	}
}
else
{
	$values = Xview::instance()->val('excluded');
	if(!empty($values))
	{
		$Ids = json_decode(stripcslashes($values),true);
		$excluded = new \Fox\Dao\SqlFragment('t.idusr NOT IN (?) ',array($Ids),array(Connection::PARAM_STR_ARRAY));
	}
}
if(isset($_POST['dbcnt']) && 1 == 1)
{
	$inputs = $searchHelp->screen()->get();
	$searchHelp->screen()->prefix('t',true);
	$fragments[] = $searchHelp->screen()->fragments();
	$finder = new User();
	if(isset($excluded))
		$fragments[] = $excluded;
	$fragments[] = new SqlFragment('w.webid = ? AND t.cloudname = ?',array(Xview::instance()->id(),
						\User::current()->cloudname()
					));
	$res = $finder->find(Fox\Dao\SqlFragment::combine($fragments),
						array('fname' => false),
						array(User::$JOIN_WEBAPPS),
						$inputs['dbcnt']);
	$searchHelp->grid()->setDatas($res);
	echo $searchHelp->grid()->Display();
}
else
{
	echo $searchHelp->display();
}
